package com.morontestandroid;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class FirstLevel extends Activity implements android.view.View.OnClickListener{
	
	ImageView banana1;
	ImageView monkey1;
	ImageView cow1;
	ImageView milk1;
	Chronometer mChronometer;
	ImageView backgr;
	TextView question;
	//public long time;
	
	boolean firstTime, milkClicked, cowClicked, bananaClicked;
	 
	 //long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_level);
        
        ClassLoader myClassLoader = Chronometer.class.getClassLoader();
	    Thread.currentThread().setContextClassLoader(myClassLoader);
        mChronometer = (Chronometer) findViewById(R.id.chronometer1);
        banana1 = (ImageView) findViewById(R.id.bananaId);
        monkey1 = (ImageView) findViewById(R.id.monkeyId);
        cow1 = (ImageView) findViewById(R.id.cowId);
        milk1 = (ImageView) findViewById(R.id.milkId);
        question = (TextView) findViewById(R.id.level1QuestionId);
        backgr = (ImageView) findViewById(R.id.level1BackgId);
        
        mChronometer.start();
        
        firstTime = true;
        milkClicked = false;
        cowClicked = false;
        bananaClicked = false;
        
        banana1.setOnClickListener(this);
       // banana1.setOnClickListener(mStartListener);
        monkey1.setOnClickListener(this);
        cow1.setOnClickListener(this);
        milk1.setOnClickListener(this);
        backgr.setOnClickListener(this);
        
        milk1.bringToFront();
       
	         
	       Typeface font = Typeface.createFromAsset(getAssets(), "testtext.ttf");  
	        question.setTypeface(font);
       
	}
	int count = 0;
	@Override
	public void onClick(View view) {
		Dialog d = new Dialog(this);
		
		if(view.getId()==R.id.level1BackgId){
			startActivity(new Intent(this, FailActivity.class));
		}
		else if(view.getId()==R.id.milkId && firstTime) {
			count++;
			
			if(count == 3) {
				milk1.setImageResource(R.drawable.milk2);
				cow1.bringToFront();
				milkClicked = true;
				count = 0;
				firstTime=false;
			}
		}
		else if(view.getId()==R.id.cowId && milkClicked){
			cowClicked = true;
			milkClicked = false;
			cow1.setImageResource(R.drawable.cow2);
			question.setVisibility(View.INVISIBLE);//hide the text.
			banana1.bringToFront();
			
		}
		
		else if(view.getId()==R.id.bananaId && cowClicked){
			bananaClicked = true;
			cowClicked = false;
			banana1.setImageResource(R.drawable.banana2);
			monkey1.bringToFront();
		}
		else if(view.getId()==R.id.monkeyId && bananaClicked){
			bananaClicked = false;
			monkey1.setImageResource(R.drawable.monkey2);
			   int activityID = 1;
			   Intent intent;
			   intent = new Intent().setClass(this, SecondLevel.class);
			   intent.putExtra("time", getTime());
			   startActivityForResult(intent, activityID);
		}
		
		else {
			startActivity(new Intent(this, FailActivity.class));
			//d = new Dialog(this);
			//d.setTitle("fail");
			//d.show();
		}
		
	}
	
	
	public void onBackPressed() {
		// do something on back.
		startActivity(new Intent(this, MenyActivity.class));
		return;
		}
	
	public long getTime(){
		  return mChronometer.getBase();
		 }

}
